<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools slot-scope="obj" :tree-node="obj.data" @delDepts="refresh" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>

      <addDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="refresh" />
    </div>
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools'
import addDept from '@/views/departments/components/add-dept'
import { getDepartments } from '@/api/departments'
import { ListToTreeData } from '@/utils/index'
export default {
  components: {
    treeTools,
    addDept

  },
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      node: {},
      loading: false // 控制进度条的显示隐藏
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 渲染组织架构的数据
    async getDepartments() {
      this.loading = true
      const res = await getDepartments()
      console.log(res)
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.departs = ListToTreeData(res.depts, '')
      this.loading = false
      // const arr = res.depts
      // // arr.forEach(item => {
      // //   console.log(item)
      // //   for (const key in item) {
      // //     console.log(item[key])
      // //   }
      // // })
      // // console.log(arr)
      // const brr = []
      // const crr = []
      // const drr = []
      // for (let i = 0; i < arr.length; i++) {
      //   // console.log(arr[i])
      //   brr.push(arr[i].id)
      //   crr.push(arr[i].pid)
      //   for (let j = 0; j < brr.length; j++) {
      //     for (let k = 0; k < crr.length; k++) {
      //       if (brr[j] === crr[k]) {
      //         if (!drr.includes(brr[i])) {
      //           drr.push(brr[j])
      //         }
      //       }
      //     }
      //   }
      // }
      // console.log(brr)
      // console.log(crr)
      // console.log(drr)
    },
    // 新增需求
    addDepts(node) {
      // console.log(84, node)
      this.showDialog = true // 显示弹框
      this.node = node
      // console.log(87, this.node)
    },
    refresh() {
      this.getDepartments()
    },
    async editDepts(node) {
      this.node = node
      await this.$refs.addDept.getDepartmentsDetail(node.id)
      this.showDialog = true // 显示弹框
    }
    // shownode(node, data) {
    //   console.log(98, node)
    //   console.log(99, data)
    // }
  }
}
</script>

<style>

</style>
